<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>添加单页</title>
    {include file="public/resource/layui_css" /}
    <link rel="stylesheet" href="/static/admin/css/style.css">
    <script type="text/javascript" charset="utf-8" src="/static/third/ueditor-v1.4.3/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/static/third/ueditor-v1.4.3/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="/static/third/ueditor-v1.4.3/lang/zh-cn/zh-cn.js"></script>
</head>

<body>
    <!-- 内容主体区域 -->
    <div class="my-form">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加单页</legend>
        </fieldset>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block my-input">
                    <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input my-text">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block my-input">
                    <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    <script id="editor" type="text/plain" style="height:500px;"></script>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">自定义路由</label>
                <div class="layui-input-block my-input">
                    <input type="text" name="route" autocomplete="off" placeholder="请输入自定义路由" class="layui-input my-text">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="addSetting">保 存</button>
                </div>
            </div>
        </form>
    </div>
    {include file="public/resource/layui_js" /}
    <script>
        let ue = UE.getEditor('editor');
        layui.use(['form', 'upload', 'element', 'laydate', 'layer'], function () {
            let $ = layui.jquery, form = layui.form, layer = layui.layer;
            // 监听提交
            form.on('submit(addSetting)', function (data) {
                let content = UE.getEditor('editor').getContent();
                if (content === '') {
                    layer.msg("请输入内容");
                    return false;
                }
                data = data.field;
                data['content'] = content;
                delete data.editorValue;
                $.ajax({
                    type: 'post',
                    url: '/admin/page/save',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    success: function (result) {
                        if (result.code === 0) {
                            layer.msg('添加成功。');
                            setTimeout(function () {
                                location.href="/admin/page";
                            }, 1000);
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>

</html>